<template>
  <div class="home-hot" ref="target">
    <HomePanel title="人气推荐" subTitle="人气爆款 不容错过">
        <!-- 主体数据模板 -->
     <template #default v-if="HotList.length">
        <ul  class="goods-list">
            <li v-for="item in HotList" :key="item.id">
                <RouterLink to="/">
                    <img v-lazyLoad="item.picture" alt="">
                    <p class="name">{{item.title}}</p>
                    <p class="desc">{{item.alt}}</p>
                </RouterLink>
            </li>
        </ul>
     </template>
        <!-- 骨架屏占位模板 -->
        <template #default v-else>
            <ul class="goods-list">
                <li v-for="item in 4" :key="item">
                    <!-- 骨架屏插件 -->
                    <XtxSkeleton :width="306" :height="406"></XtxSkeleton>
                <!-- 表达式：字面量，对象，setup()里面的函数 -->
                </li>
            </ul>
        </template>
    </HomePanel>
  </div>
</template>

<script>
import { getHotListAPI } from '@/api/home'
import HomePanel from './home-panel.vue'
import { ref } from 'vue'
// 导入useWatchDOM组合式方法
import { useWatchDOM } from '@/compositions/usewatchdom'
export default {
  name: 'HomeHot',
  components: { HomePanel },
  setup () {
    // 人气列表
    const HotList = ref([])
    // 调用自己的监听DOM方法
    // target 是ref 对象
    const target = useWatchDOM(getHotList)
    // 获取人气列表数据函数
    async function getHotList () {
      // 发起获取人气列表数据请求
      const res = await getHotListAPI()
      HotList.value = res.result
    }
    return {
      HotList,
      target
    }
  }

}
</script>

<style scoped lang='less'>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
</style>
